set google(agent) "MSIE 6.0"

proc google {type conf serv nick text} {
	global google
	set a "google"
	if {[llength $text]==0} {
		::debug $a google_text more_than_0
		if {$type=="groupchat"} {
			gmsg $conf $serv "$nick" "Usage: google <string>"
			return
		} else {
			pmsg $conf $serv "$nick" "Usage: google <string>"
			return
		}
	}
	set url "http://www.google.com/search?btnI=&q="
	set output [google:go $url $text]
   
	if {$type=="groupchat"} {
		gmsg $conf $serv "$nick" "$output"
		return
	} else {
		pmsg $conf $serv "$nick" "$output"
		return
	}
}

proc google:go { url arg } {
	global google
	set a "google:go"
	regsub -all " " $arg "+" query
	set lookup "$url$query"
	set token [http::config -useragent "$google(agent)"]
	::debug $a agent "$google(agent)"
	set token [http::geturl $lookup]
	::debug $a token_geturl $token
#  puts stderr ""
	upvar #0 $token state
	putlog "upvar #0 token state====token===> $token"
	set max 0
	foreach {name value} $state(meta) {
	::debug $a name "$name"
	::debug $a value "$value"
		if {[regexp -nocase ^location$ $name]} {
			set newurl [string trim $value]
			regsub -all "btnI=&" $url "" url
			::debug $a url "$url"
			::debug $a query "$query"
			return "$newurl More: $url$query"
		}
	}
}
